package com.leyou.server;

import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.exception.pojo.LyException;
import com.leyou.item.pojo.Sku;
import com.leyou.mapper.SkuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ItemService {
    @Autowired(required = false)
    SkuMapper skuMapper;
    public Long saveItem(Long id) {
        if (id.equals(1L)){
            throw new LyException(ExceptionEnum.CARTS_NOT_FOUND);
        }
        return id;
    }

    public void minusStock(Map<Long, Integer> paramap) {
        paramap.entrySet().forEach(
             entry->{
                 Long skuId = entry.getKey();
                 Integer num = entry.getValue();
                 Sku sku = skuMapper.selectById(skuId);
                 if (sku.getStock()>= num){
                     sku.setStock(sku.getStock()-num);
                     skuMapper.updateById(sku);
                 }

             }
        );
    }
}
